package cn.itcast.springboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    public static final String QUEUE1="pubsub_log";
    public static final String QUEUE2="pubsub_baojing";
    public static final String QUEUE3="queue_TTL";
    public static final String QUEUE4="queue_DLX";
    public static final String EXCHANGE="bajing_exchange";


    @Bean(QUEUE3)
    public Queue getQueue3(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl",50000);  //队列存活时间
        map.put("x-max-length",5);  //队列最大长度
        map.put("x-dead-letter-exchange",EXCHANGE);  //死信队列绑定
        map.put("x-dead-letter-routing-key","dlx");  //死信队列绑定
        return new Queue(QUEUE3,true,false,false,map);
    }

    @Bean(QUEUE4)
    public Queue getQueue4(){
        return new Queue(QUEUE4,true,false,false);
    }

    @Bean(QUEUE1)
    public Queue getQueue1(){
        return new Queue(QUEUE1,true,false,false);
    }

    @Bean(QUEUE2)
    public Queue getQueue2(){
        return new Queue(QUEUE2,true,false,false);
    }

    @Bean(EXCHANGE)
    public Exchange getExchange(){
        return ExchangeBuilder.directExchange(EXCHANGE).durable(true).build();
    }


    //绑定关系
    @Bean
    public Binding queue4_exchange(@Qualifier(QUEUE4) Queue queue,
                                   @Qualifier(EXCHANGE) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }
    @Bean
    public Binding queue3_exchange(@Qualifier(QUEUE3) Queue queue,
                                   @Qualifier(EXCHANGE) Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }
    @Bean
    public Binding queue1_exchange(@Qualifier(QUEUE1) Queue queue,
                                   @Qualifier(EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("error").noargs();
    }
    @Bean
    public Binding queue2_exchange(@Qualifier(QUEUE2) Queue queue,
                                   @Qualifier(EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("info").noargs();
    }
    @Bean
    public Binding queue2_exchange1(@Qualifier(QUEUE2) Queue queue,
                                   @Qualifier(EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("error").noargs();
    }
    @Bean
    public Binding queue2_exchange2(@Qualifier(QUEUE2) Queue queue,
                                   @Qualifier(EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("warning").noargs();
    }

}
